import Axios from 'axios'

const http = Axios.create({
  baseURL: 'http://127.0.0.1:8888/api/private/v1/',
  timeout: 3000
})

http.interceptors.request.use(config => {
  // 为请求头添加 Authorization 字段添加token值
  config.headers['Authorization'] = window.sessionStorage.getItem('token')
  return config
})

http.interceptors.response.use(response => {
  return response
})

export const createAPI = (url, method, data) => {
  if (method.toUpperCase() === 'GET') {
    return http.get(url, {
      params: { ...data }
    })
  } else return http.post(url, data)
}

export default http
